package cn.powertime.evaluation.app.controller;

import cn.powertime.evaluation.Constants;
import cn.powertime.evaluation.entity.CircleUser;
import cn.powertime.evaluation.entity.CpsCircle;
import cn.powertime.evaluation.entity.SysUser;
import cn.powertime.evaluation.idempotent.ApiIdempotent;
import cn.powertime.evaluation.service.CircleUserService;
import cn.powertime.evaluation.service.CpsCircleSerivce;
import cn.powertime.evaluation.service.SysUserService;
import cn.powertime.evaluation.shiro.ShiroUtils;
import cn.powertime.evaluation.vo.ChapterVo;
import cn.powertime.evaluation.vo.CpsCircleVo;
import cn.powertime.evaluation.vo.CpsCircleVoS;
import cn.powertime.evaluation.vo.ShiroUserInfo;
import cn.powertime.evaluation.web.controller.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.powertime.id.IdWorker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/app/cirle")
@Api(value = "/app/cirle", tags = "圈子接口", produces = MediaType.ALL_VALUE)
public class AppCpsCircleController extends BaseController{

    @Autowired
    private CpsCircleSerivce cpsCircleSerivce;

    @Autowired
    private CircleUserService circleUserService;

    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/addCirle")
    @ApiOperation(value = "创建圈子")
    public  Object add(@Validated @RequestBody CpsCircleVo cpsCircleVo){
        ShiroUserInfo userInfo = ShiroUtils.getUserInfo();
        CpsCircle cpsCircle =new CpsCircle();
        cpsCircle.setDict(cpsCircleVo.getDict());
        cpsCircle.setCircleName(cpsCircleVo.getCircleName());
        Long id=IdWorker.getId();
        cpsCircle.setId(id);
        cpsCircle.setUserId(userInfo.getId());
        cpsCircle.setStatus(Constants.STATUS_NORMAL);
        cpsCircle.setCreateTime(LocalDateTime.now());
        cpsCircle.setUpdateTime(LocalDateTime.now());
        boolean b=cpsCircleSerivce.ownSave(cpsCircle);
        String userIds=cpsCircleVo.getUserId();
        String[] arry=userIds.split(",");
        CircleUser circleUser = new CircleUser();
        for(int i= 0;i<arry.length;i++){
            String ids=arry[i];
            Long userId=Long.parseLong(ids);
            circleUser.setId(IdWorker.getId());
            circleUser.setCircleId(id);
            circleUser.setUserId(userId);
            circleUser.setCreateTime(LocalDateTime.now());
            circleUser.setUpdateTime(LocalDateTime.now());
            boolean b1=circleUserService.ownSave(circleUser);
            if(!b1){
                return error("失败");
            }
        }
        circleUser.setId(IdWorker.getId());
        circleUser.setCircleId(id);
        circleUser.setUserId(userInfo.getId());
        //circleUser.setUserId(4L);
        circleUser.setCreateTime(LocalDateTime.now());
        circleUser.setUpdateTime(LocalDateTime.now());
        boolean b2=circleUserService.ownSave(circleUser);
        if(!b2){
            return error("失败");
        }
        if(b){
            return success("成功");
        }
        return error("失败");
    }

    @PostMapping("/deleteCirle")
    public  Object deletet(Long id){
        ShiroUserInfo userInfo = ShiroUtils.getUserInfo();
        QueryWrapper<CpsCircle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",Constants.STATUS_NORMAL);
        queryWrapper.eq("id",id);
        queryWrapper.eq("user_id",userInfo.getId());
        int i=cpsCircleSerivce.count();
        if(i==0){
            return error("不是自己创建的圈子不可删除");
        }
        CpsCircle cpsCircle= new CpsCircle();
        cpsCircle.setId(id);
        cpsCircle.setStatus(Constants.STATUS_DEL);
        boolean b=cpsCircleSerivce.ownUpdateById(cpsCircle);
        if(b){
            return success("成功");
        }
        return error("失败");
    }

    @ApiOperation(value = "查询所有创建圈子")
    @PostMapping(value ="/list")
    public  Object List(@Validated @RequestBody  ChapterVo vo){
        Page page=PageHelper.startPage(vo.getPage(),vo.getSize());
        QueryWrapper<CpsCircle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", Constants.STATUS_NORMAL);
        queryWrapper.orderByDesc("create_time") ;
        List<CpsCircle> cpsCircleList =cpsCircleSerivce.list(queryWrapper);
        List<CpsCircleVoS> list=new ArrayList<>();
        CpsCircleVoS cpsCircleVoS;
        ShiroUserInfo userInfo = ShiroUtils.getUserInfo();
        for(int i=0;i<cpsCircleList.size();i++){
            cpsCircleVoS= new CpsCircleVoS();
            Long userId=cpsCircleList.get(i).getUserId();
            cpsCircleVoS.setType(2);



            cpsCircleVoS.setId(cpsCircleList.get(i).getId());
            cpsCircleVoS.setCircleName(cpsCircleList.get(i).getCircleName());
            cpsCircleVoS.setCreateTime(cpsCircleList.get(i).getCreateTime());
            cpsCircleVoS.setDict(cpsCircleList.get(i).getDict());
            cpsCircleVoS.setStatus(cpsCircleList.get(i).getStatus());
            cpsCircleVoS.setUserId(userId);
            QueryWrapper<CircleUser> queryWrappers = new QueryWrapper<>();
            queryWrappers.eq("status", Constants.STATUS_NORMAL);
            queryWrappers.eq("circle_id",cpsCircleList.get(i).getId());
            List<CircleUser> list1=circleUserService.list(queryWrappers);
            for (int j =0;j<list1.size();j++){
                if(list1.get(j).getUserId().equals(userInfo.getId())){
                    cpsCircleVoS.setType(1);
                }
            }
            cpsCircleVoS.setCount(list1.size());
            list.add(cpsCircleVoS);
        }
        PageInfo pageInfo = new PageInfo(list);
        return success1(list,pageInfo.getPages(),pageInfo.getPageSize(), page.getTotal());
    }

    @PostMapping("/update")
    public  Object update(CpsCircle cpsCircle){
        cpsCircle.setUpdateTime(LocalDateTime.now());
        boolean b =cpsCircleSerivce.ownUpdateById(cpsCircle);
        if(b){
            return success("成功");
        }
        return error("失败");
    }


    @GetMapping("/joinCircle")
    @ApiOperation(value = "加入圈子")
    @ApiImplicitParam(name = "circleId", value = "圈子id", required = true, dataType = "Long")
    public  Object addCircleUser(@NotNull(message = "圈子id不能为空") @RequestParam Long circleId){
        ShiroUserInfo userInfo = ShiroUtils.getUserInfo();
        QueryWrapper<CircleUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", Constants.STATUS_NORMAL);
        queryWrapper.eq("user_id",userInfo.getId());
        queryWrapper.eq("circle_id",circleId);
       //queryWrapper.eq("user_id",1);
        int i=circleUserService.count(queryWrapper);
        if(i>0){
            return error("已经加过此圈子");
        }
        CircleUser circleUser = new CircleUser();
        circleUser.setId(IdWorker.getId());
        circleUser.setCircleId(circleId);
        circleUser.setUserId(userInfo.getId());
        //circleUser.setUserId(1L);
        circleUser.setCreateTime(LocalDateTime.now());
        circleUser.setUpdateTime(LocalDateTime.now());
        boolean b= circleUserService.ownSave(circleUser);
        if(b){
            return success("成功");
        }
        return error("失败");
    }

    @PostMapping("/user")
    @ApiOperation(value = "所有用户",response = SysUser.class)
    public Object selectUser(@Validated @RequestBody  ChapterVo vo){
       // Page page1 =PageHelper.startPage(page,size);
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",Constants.STATUS_CREDIT).or().eq("status",Constants.STATUS_RESET_PWD);
        List<SysUser> sysUsers=sysUserService.list(queryWrapper);
        PageInfo pageInfo = new PageInfo(sysUsers);
       // return success1(sysUsers,pageInfo.getPages(),pageInfo.getPageSize(), page1.getTotal());
        return success(sysUsers);
    }


}
